//
//  DocumentationBootcamp.swift
//  FCSwiftUI
//
//  Created by 冯宣超 on 2023/3/4.
//

import SwiftUI

struct DocumentationBootcamp: View {
    // MARK: Swift的注释
    @State var data: [String] = [
        "Apples", "Oranges", "Bananas"
    ]
    @State var showAlert: Bool = false
    
    // Working copy - things to do:
    // 1) Fix title
    // 2) Fix alert
    // 3) Fix something else
    
    /*
     Working copy - things to do:
     1) Fix title
     2) Fix alert
     3) Fix something else
     */
    
    var body: some View {
        NavigationStack {
            ZStack {
                // 最下面一层
                Color.red.ignoresSafeArea()
                
                // 最上面一层的容器
                foregroundLayer
                .navigationTitle("Documentation")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Alert") {
                            showAlert.toggle()
                        }

                    }
                }
                .alert(isPresented: $showAlert, content: {
                    getAlert(text: "This is the alert!")
                })
//                .alert("This is the alert NEW METHOD!", isPresented: $showAlert, presenting: nil) {
//
//                }
            }
        }
    }
    
    /// This is the foreground layer that holds a scrollView
    private var foregroundLayer: some View {
        ScrollView {
            Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
            
            ForEach(data, id: \.self) { name in
                Text(name)
                    .font(.headline)
            }
        }
    }
    
    // Command + 点击 -> Add Documentation
    /// Gets an Alert with a specified title
    ///
    /// This function creates and returns an alert immediately. The alert will have a title based on the text parameter but it will NOT have a message
    ///  ```
    ///  getAlert(text:  "Hi") -> Alert(title: Text("Hi"))
    ///  ```
    ///
    /// - Warning: There is no additional message in this Alert.
    /// - Parameter text: This is the title for the alert.
    /// - Returns: Returns an alert with a title.
    func getAlert(text: String) -> Alert {
        return Alert(title: Text(text))
    }
}

struct DocumentationBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        DocumentationBootcamp()
    }
}
